<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 下载页模型
 * @author LM
 */
class Down_home_model extends MY_Model {
	function __construct(){
		parent::__construct();
	}
	
	/**
	 * 获取指定文件分享信息 不包括内容
	 */
	public function get_share_link($uuid){
		$where = array(
				'uuid'  => $uuid
		);
		$share_result = $this->db->get_where('sharelink', $where)->row_array();
		return $share_result;
	}
	
	/**
	 * 更新访问次数
	 */
	public function update_count($share_result){
		$where = array(
				'id' => $share_result['id']
		);
		$data = array(
				'download_count' => $share_result['download_count'] + 1
		);
		$this->db->where($where)->update('sharelink', $data);
	}
	
	/**
	 * 获取指定文件分享内容
	 */
	public function get_share_list($sid){
		$where = array(
				'lid'  => $sid
		);
		
		$share_list_result = $this->db->get_where('sharelist', $where)->result_array();
		return $share_list_result;
	}
	
	/**
	 * 获取分享的文件或文件夹数据
	 */
	public function get_share_file_data($uuid_arr, $type = 1){
		$where = array(
				'access_denied' => 0,
				'deleted' => 0
		);
		$where_in = $uuid_arr;
		if($type == 2){
			$tableName = 'folder';
			$field = 'id, pid, uuid, name, creation_ts';
		} else {
			$tableName = 'file';
			$field = 'file.id, file.pid, file.uuid, file.name, file.creation_ts, file.type, file_type.ext_name, data_file.rid, data_file.data_size, data_file.expiration_ts';
		}
		$this->db->select($field);
		$this->db->where($where)->where_in('uuid', $where_in);
		if($type == 1){
			$this->db->join('file_type', 'file_type.id=file.type');
			$this->db->join('data_file', 'data_file.hash_file_uuid=file.hash_secure_box_uuid', 'left');
		}
		$result = $this->db->order_by('id', 'DESC')->get($tableName)->result_array();
		
		return $result;
	}
	
	/**
	 * 获取指定目录id和uid
	 */
	private function get_folder_id($uuid){
		$where = array(
				'uuid' => $uuid
		);
		//获取该目录id
		$result = $this->db->select('id, uid')->get_where('folder', $where)->row_array();
		if(!$result){
			return false;
		}
		return $result;
	}
	
	/**
	 * 获取指定目录下的文件夹及文件 不包括禁止访问和被删除的
	 */
	public function get_folder_file($uuid){
		$folder = $this->get_folder_id($uuid);
		if(!$folder){
			return false;
		}
		$where = array(
				'pid' => $folder['id'],
				'access_denied' => 0,
				'deleted' => 0
		);
		//查找目录下文件夹
		$folder_field = 'id, pid, uuid, name, creation_ts';
		$folder_result = $this->db->select($folder_field)->order_by('id', 'DESC')->get_where('folder', $where)->result_array();
		foreach($folder_result as $k => $v){
			$folder_result[$k]['folder'] = 1;
		}
		//查找目录下文件
		$file_result = $this->common_get_file($where);
		$f_result = array_merge($folder_result, $file_result);
		return $f_result;
	}
	
	/**
	 * 获取文件信息公共方法
	 */
	private function common_get_file($where){
		$file_field = 'file.id, file.pid, file.uuid, file.name, file.creation_ts, file.type, file_type.ext_name, data_file.rid, data_file.data_size, data_file.expiration_ts';
		$this->db->select($file_field);
		$this->db->join('file_type', 'file_type.id=file.type');
		$this->db->join('data_file', 'data_file.hash_file_uuid=file.hash_secure_box_uuid');
		$file_result = $this->db->order_by('file.id', 'DESC')->get_where('file', $where)->result_array();
		if(!$file_result){
			return $file_result;
		}
		$file_result = $this->convert_file_size($file_result);
		return $file_result;
	}
	
	/**
	 * 获取指定文件在文件表中的数据
	 */
	public function get_filetable_data($uuid, $field=''){
		$where = array(
				'uuid' => $uuid
		);
		$field != '' &&  $this->db->select($field);
		return $this->db->get_where('file', $where)->row_array();
	}
	
	/**
	 * 批量更新文件数据 
	 */
	public function update_file($data){
		$this->db->update_batch('file',$data,'uuid');
	}
	
	/**
	 * 获取路径
	 */
	public function get_path($share_pid, $uuid){
		$folder = $this->get_folder_id($uuid);
		$folder_field = 'id, pid, uuid, name, creation_ts';
		$where = array(
				'uid' => $folder['uid']
		);
		$folder_all = $this->db->select($folder_field)->order_by('id', 'DESC')->get_where('folder', $where)->result_array();
		$parent = self::getParents($folder_all, $folder['id'], $share_pid);
		return $parent;
	}
	
	/**
	 * 获取指定文件夹下所有文件 不包括禁止访问和删除的
	 */
	public function get_folder_all_file($uuid){
		$folder = $this->get_folder_id($uuid);
		$where = array(
				'uid' => $folder['uid'],
				'access_denied' => 0,
				'deleted' => 0
				);
		$folder_result = $this->db->order_by('id', 'DESC')->select('id, pid')->get_where('folder', $where)->result_array();
		foreach($folder_result as $k => $v){
			$folder_result[$k]['folder'] = 1;
		}
		$file_field = 'file.id, file.pid, file.uuid, data_file.rid, data_file.data_size';
		$this->db->select($file_field);
		$this->db->join('data_file', 'data_file.hash_file_uuid=file.hash_secure_box_uuid');
		$file_result = $this->db->order_by('file.id', 'DESC')->get_where('file', $where)->result_array();
		$file_result = $this->convert_file_size($file_result);
		$result = array_merge($folder_result, $file_result);
		$childs = self::getChilds($result, $folder['id']);
		return $childs;
	}
	
	/**
	 * 获取下载文件名
	 * @param array $rids_arr
	 */
	public function get_down_filename($rids_arr){
		$this->db->select('file.name,file.id');
		$this->db->join('file', 'file.hash_secure_box_uuid=data_file.hash_file_uuid');
		return $this->db->order_by('file.id','DESC')->where_in('data_file.rid', $rids_arr)->get('data_file')->result_array();
	}
	
	/**
	 * 文件大小转换
	 */
	public function convert_file_size($f_result){
		foreach($f_result as $k => $v){
			$f_result[$k]['data_size'] = Common::get_file_size($v['data_size']);
		}
		return $f_result;
	}
	
	/**
	 * 传递子分类的id返回所有的父级分类
	 */
	static function getParents($cate, $id, $share_pid){
		$arr = array();
		foreach($cate as $v){
			if($v['id'] == $id){
				$arr[] = $v;
				//到分享的pid为止
				if($v['pid'] == $share_pid){
					break;
				}
				$arr = array_merge(self::getParents($cate, $v['pid'], $share_pid), $arr);
			}
		}
		return $arr;
	}
	
	/**
	 * 传递父级id返回所有子级分类 不包含文件夹
	 */
	static function getChilds($result, $pid){
		$arr = array();
		foreach($result as $v){
			if($v['pid'] == $pid){
				if(!isset($v['folder'])){
					$arr[] = $v;
				} else {
					$arr = array_merge($arr, self::getChilds($result, $v['id']));
				}
			}
		}
		return $arr;
	}
}
